var angular = angular;
var console = console;

homeApp.controller('apk2Controller', ['$scope', '$http', '$filter', '$modal', function ($scope, $http, $filter, $modal) {
    'use strict';
    
    $scope.maxSize = 3;
    $scope.bigCurrentPage = 1;
    
    $scope.totalstreams     = 10;
    
    $scope.nodata           = true;
    $scope.nodatalocation   = true;
    $scope.loading          = true;
    $scope.loadinglocations = true;
    
    $scope.menu_area        = [];
    $scope.menu_rayon       = [];
    $scope.menu_penyulang   = [];
    
    $http.get('api/penyulang')
        .success(function (data) {
            //console.log(data);
            if (data.response === "OK" && data.message === "Get all penyulang data success.") {
                $scope.loading  = false;
                $scope.datapenyulang    = _.sortBy(_.sortBy(data.result.penyulang, function(some) { return some.tanggal}).reverse(), function(some) { return some.area + "#" + some.rayon + "#" + some.feeder});
                
//                $scope.streams          = $scope.datapenyulang.slice(0, $scope.totalstreams);
                $scope.nodata = false;
            } else {
                $scope.loading  = false;
                console.log(data);
            }
        }).error(function (data) {
            $scope.loading  = false;
            console.log(data);
        });
    
    $http.get('api/locations')
        .success(function (data) {
//            console.log(data);
            if (data.response === "OK" && data.message === "Get all data locations success.") {
                $scope.loadinglocations  = false;
                
                $scope.locations = data.result.locations;
                
                _.each($scope.locations, function(value, key) {
                    $scope.menu_area.push(key);
                });
//                console.log($scope.locations.Kendari['Wuawua']);
            } else {
                $scope.loadinglocations  = false;
                console.log(data);
            }
        }).error(function (data) {
            $scope.loadinglocations  = false;
            console.log(data);
        });
    
    $scope.$watch('bigCurrentPage', function (newVal, oldVal) {
        if ((newVal !== oldVal) && (typeof(newVal) !== 'undefined') && (typeof(oldVal) !== 'undefined')) {
            //console.log($scope.bigCurrentPage);
            $scope.streams              = $scope.sortedGroupedDatashown.slice(($scope.bigCurrentPage - 1) * $scope.totalstreams, $scope.bigCurrentPage * $scope.totalstreams);
        }
    });
        
    $scope.$watch('location_area', function (newVal, oldVal) {
        if ((newVal !== oldVal) && (typeof(newVal) !== 'undefined') && (typeof(oldVal) !== 'undefined')) {
            if (newVal === 'Area') {
                $scope.menu_rayon = [];
            } else {
                _.each($scope.locations[$scope.location_area], function(value, key) {
                    $scope.menu_rayon.push(key);
                });
            }
        }
    });
    
    $scope.$watch('location_rayon', function (newVal, oldVal) {
        if ((newVal !== oldVal) && (typeof(newVal) !== 'undefined') && (typeof(oldVal) !== 'undefined')) {
            if (newVal === 'Rayon') {
                $scope.menu_penyulang = [];
            } else {
                _.each($scope.locations[$scope.location_area][$scope.location_rayon], function(value, key) {
                    $scope.menu_penyulang.push(value);
                });
            }
        }
    });
    
    $scope.makeTanggal = function(some) {
        return Date.create(some).format('{dd} {Mon} {yyyy}') + "\n" + Date.create(some).format('{hh}:{mm}');
    }
    
    $scope.changeArea = function(some) {
        $scope.nodatalocation       = true;
        $scope.location_rayon       = 'Rayon';
        $scope.location_penyulang   = 'Penyulang';
        
        if (some !== $scope.location_area) {
            $scope.location_area        = some;
            $scope.menu_rayon           = [];
            $scope.menu_penyulang       = [];
        }
        $scope.changeDataTable();
        
    }
    $scope.changeRayon = function(some) {
        $scope.nodatalocation       = true;
        $scope.location_penyulang   = 'Penyulang';
        if (some !== $scope.location_rayon) {
            $scope.menu_penyulang       = [];
            $scope.location_rayon       = some;
        }
        $scope.changeDataTable();
    }
    $scope.changePenyulang = function(some) {
        $scope.location_penyulang   = some;
        $scope.changeDataTable();
    }
    $scope.openModal = function(some) {
//        console.log(some);
        var modalInstance = $modal.open({
            templateUrl: 'fullTable.html',
            controller: 'fullTableController',
            backdropClass : 'bacdropAdditionClass',
            windowClass     : 'modalTableFullClass',
            resolve: {
                params: function () {
                    return some;
                }
            }
        });
    }
    $scope.tryingtoexport = function(some) {
        return JSON.stringify(some);
    }
    $scope.changeDataTable = function() {
        if ($scope.location_rayon === 'Rayon') {
            $scope.filter_rayon = "";
        } else {
            $scope.filter_rayon = $scope.location_rayon;
        }
        if ($scope.location_penyulang === 'Penyulang') {
            $scope.filter_penyulang = "";
        } else {
            $scope.filter_penyulang = $scope.location_penyulang;   
        }
        $scope.datashown    =  $filter('filter')($scope.datapenyulang, {area : $scope.location_area, rayon : $scope.filter_rayon, feeder : $scope.filter_penyulang})
        
        $scope.groupedDatashown = _.groupBy($scope.datashown, function(some) {return some.nama_gardu + "#" + some.area + "#" + some.rayon + "#" + some.feeder});
        
        $scope.sortedGroupedDatashown = [];
        
        _.each($scope.groupedDatashown, function(some, key) {
//            console.log(some);
            $scope.sortedGroupedDatashown.push((_.sortBy(some, function(somesome) {return somesome.tanggal;})).reverse());
        });
        
//        console.log($scope.groupedDatashown);
//        console.log($scope.sortedGroupedDatashown);
        
        if ($scope.datashown.length === 0) {
            $scope.nodatalocation   = true;
        } else {
            $scope.nodatalocation   = false;
            $scope.streams          = $scope.sortedGroupedDatashown.slice(0, $scope.totalstreams);
        }
    }
}]);

homeApp.controller('fullTableController',['$scope','$modal','params', function($scope,$modal,params) {
    $scope.streams = params;
    
    $scope.makeTanggal = function(some) {
        return Date.create(some).format('{dd} {Mon} {yyyy}') + "\n" + Date.create(some).format('{hh}:{mm}');
    }
//    console.log(params);
    
}]);

homeApp.directive('myRepeatDirective', function () {
    'use strict';
    return function (scope, element, attrs) {
        if (scope.$last) {
            $(".table-body").scroll(function () {
                $(".table-header").offset({ left: (-1 * this.scrollLeft) + 41 });
            });
        }
    };
});
homeApp.directive('anotherRepeatDirective', function () {
    'use strict';
    return function (scope, element, attrs) {
        if (scope.$last) {
            $(".table-body-modal").scroll(function () {
                $(".table-header-modal").offset({ left: (-1 * this.scrollLeft) + 216 });
            });
        }
    };
});
